package com.up.management.service.impl;

import com.up.management.domain.entity.Event;
import com.up.management.mapper.EventMapper;
import com.up.management.service.IEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 活动管理Service业务层处理
 * 
 * @author cloud
 * @date 2025-10-09
 */
@Service
public class EventServiceImpl implements IEventService 
{
    @Autowired
    private EventMapper eventMapper;

    @Autowired
    private com.up.management.mapper.EventDetailMapper eventDetailMapper;

    @Override
    public com.up.management.domain.vo.EventDetailVO selectEventDetailById(Long id) {
        return eventMapper.selectEventDetailById(id);
    }

    /**
     * 查询活动管理
     * 
     * @param id 活动管理主键
     * @return 活动管理
     */
    @Override
    public Event selectEventById(Long id)
    {
        return eventMapper.selectEventById(id);
    }

    /**
     * 查询活动管理列表
     * 
     * @param event 活动管理
     * @return 活动管理
     */
    @Override
    public List<Event> selectEventList(Event event)
    {
        return eventMapper.selectEventList(event);
    }

    /**
     * 新增活动管理
     * 
     * @param event 活动管理
     * @return 结果
     */
    @Override
    public int insertEvent(Event event)
    {
        return eventMapper.insertEvent(event);
    }

    @Override
    public int insertEventWithDetail(com.up.management.domain.dto.EventWithDetailDTO eventWithDetail) {
        int result = eventMapper.insertEvent(eventWithDetail);
        if (result > 0 && eventWithDetail.getEventDetail() != null) {
            com.up.management.domain.entity.EventDetail detail = eventWithDetail.getEventDetail();
            detail.setEventId(eventWithDetail.getId());
            normalizeImagesVideos(detail);
            int d = eventDetailMapper.insertEventDetail(detail);
            if (d <= 0) return 0;
        }
        return result;
    }

    /**
     * 修改活动管理
     * 
     * @param event 活动管理
     * @return 结果
     */
    @Override
    public int updateEvent(Event event)
    {
        return eventMapper.updateEvent(event);
    }

    @Override
    public int updateEventWithDetail(com.up.management.domain.dto.EventWithDetailDTO eventWithDetail) {
        int result = eventMapper.updateEvent(eventWithDetail);
        if (result > 0 && eventWithDetail.getEventDetail() != null) {
            com.up.management.domain.entity.EventDetail detail = eventWithDetail.getEventDetail();
            detail.setEventId(eventWithDetail.getId());
            normalizeImagesVideos(detail);
            com.up.management.domain.entity.EventDetail existing = eventDetailMapper.selectEventDetailByEventId(eventWithDetail.getId());
            if (existing != null) {
                detail.setId(existing.getId());
                eventDetailMapper.updateEventDetail(detail);
            } else {
                eventDetailMapper.insertEventDetail(detail);
            }
        }
        return result;
    }

    private void normalizeImagesVideos(com.up.management.domain.entity.EventDetail detail) {
        detail.setImages(processStringToJsonArray(detail.getImages()));
        detail.setVideos(processStringToJsonArray(detail.getVideos()));
    }

    private String processStringToJsonArray(String str) {
        if (str == null || str.trim().isEmpty()) return "[]";
        String s = str.trim();
        try {
            if (s.startsWith("[") && s.endsWith("]")) {
                // valid JSON array string, return normalized
                com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
                java.util.List<String> arr = mapper.readValue(s, new com.fasterxml.jackson.core.type.TypeReference<java.util.List<String>>(){});
                return mapper.writeValueAsString(arr);
            } else {
                String[] parts = s.split(",");
                java.util.List<String> list = new java.util.ArrayList<>();
                for (String p : parts) {
                    String t = p.trim();
                    if (!t.isEmpty()) list.add(t);
                }
                com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
                return mapper.writeValueAsString(list);
            }
        } catch (Exception e) {
            return "[]";
        }
    }

    /**
     * 批量删除活动管理
     * 
     * @param ids 需要删除的活动管理主键
     * @return 结果
     */
    @Override
    public int deleteEventByIds(Long[] ids)
    {
        return eventMapper.deleteEventByIds(ids);
    }

    /**
     * 删除活动管理信息
     * 
     * @param id 活动管理主键
     * @return 结果
     */
    @Override
    public int deleteEventById(Long id)
    {
        return eventMapper.deleteEventById(id);
    }
}
